package com.riaevolution.components
{
    import mx.collections.ArrayCollection;
    import mx.collections.Sort;
    import mx.events.CollectionEvent;
    import mx.controls.Alert;
    
    public class LocalPaginator {   
	    
	    public function LocalPaginator(source:ArrayCollection=null){
	        super();
	        if(source){
	        	_collection = source;
	        	_collection.addEventListener(CollectionEvent.COLLECTION_CHANGE,detectChange);
	        }
	        pages = 0;
	        setFilter();
	    }
        private var _collection:ArrayCollection;
        //Private variables
        private var _pages:Number;
        private var _itemsPerPage:Number= 20;
        private var _currentPage:Number = 1;
        private var _maxIndex:Number = 24;
        private var _minIndex:Number = 0;
        private var _externalChange:Boolean = true;
		[Bindable] public var recentMsj:String = "";
        private function detectChange(event:CollectionEvent):void {
        	if(_externalChange){
        		recentMsj = "Cambio el largo a: "+this._collection.length;
        		trace(recentMsj);
        	}
        }
        //private methods
        
        
        private function setFilter():void{
			this._maxIndex = (this.currentPage * this.itemsPerPage)-1;
            this._minIndex = this._maxIndex - (this.itemsPerPage -1);
            this._collection.filterFunction = this.filterData;
            this._collection.refresh();
        }
        
        private function filterData(item:Object):Boolean{
        	return (this._collection.getItemIndex(item) >= _minIndex && this._collection.getItemIndex(item) <= _maxIndex)
        }
        
        //public methods
        [Bindable]
        public function get itemsPerPage():Number{
            return _itemsPerPage;
        }
        public function set itemsPerPage(value:Number):void{
            _itemsPerPage = value;
            pages = 0;
            if(this.currentPage > this.pages){
                this.currentPage = this.pages;
            }
            setFilter();
        }
        
        [Bindable]
        public function get collection():ArrayCollection{
            return _collection;
        }
        public function set collection(arrayCollection:ArrayCollection):void{
            _collection = arrayCollection;
            setFilter();
        }
        
        [Bindable]
        public function get currentPage():Number{
            return _currentPage;
        }
        public function set currentPage(value:Number):void{
            _currentPage = value;
			setFilter();
        }
        
        [Bindable]
        public function get pages():Number{
            return _pages;
        }
        public function set pages(value:Number):void{
           if(this._collection.length % this.itemsPerPage == 0){
               _pages = this._collection.length/this.itemsPerPage;
           }
           else{
               _pages =  int(this._collection.length/this.itemsPerPage)+1;
           }
        }
        
        public function pageUp():void{
            if(this.currentPage < this.pages){
                this.currentPage += 1;
            }
        }
        public function pageDown():void{
            if(this.currentPage > 1){
                this.currentPage -= 1;
            }
        }    
    }
}